// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

using System;
using System.ServiceModel;
using System.Xml;
using System.Globalization;

namespace SL.IdentityModel.Protocols.WSTrust
{
    /// <summary>
    /// Class performs WSTrust 13 RequestSecurityToken serialization to XML
    /// </summary>
    internal class WSTrust13SerializationHelper
    {
        private const string Address = "Address";
        private const string AppliesTo = "AppliesTo";
        private const string EndpointReference = "EndpointReference";
        private const string nsAddressing = "http://www.w3.org/2005/08/addressing";
        private const string nsPolicy = "http://schemas.xmlsoap.org/ws/2004/09/policy";
        private const string prefixAddressing = "a";
        private const string prefixPolicy = "wsp";


        /// <summary>
        /// Writes RequestSecurityToken to writer
        /// </summary>
        /// <param name="rst">RequestSecurityToken to be written to XML</param>
        /// <param name="writer">XmlWriter XML is written to</param>
        public static void WriteRST( RequestSecurityToken rst, XmlWriter writer )
        {
            if ( rst.AppliesTo != null )
            {
                WriteAppliesTo( writer, rst.AppliesTo );
            }
            if ( !string.IsNullOrEmpty( rst.RequestType ) )
            {
                WriteRequestType( writer, rst.RequestType );
            }
            if ( null != rst.EntropyBytes )
            {
                WriteEntropy( writer, rst.EntropyBytes );
            }
            if ( !string.IsNullOrEmpty( rst.KeyType ) )
            {
                WriteKeyType( writer, rst.KeyType );
            }
            if ( null != rst.KeySizeInBits && rst.KeySizeInBits.HasValue )
            {
                WriteKeySizeInBits( writer, rst.KeySizeInBits.Value );
            }
        }

        internal void WriteKnownRequestElement( RequestSecurityToken rst, XmlWriter writer )
        {
            WriteRST( rst, writer );
        }

        internal void WriteXml( RequestSecurityToken request, System.Xml.XmlWriter writer )
        {
            WriteRequest( request, writer );
        }

        public void WriteRequest( RequestSecurityToken rst, XmlWriter writer )
        {
            writer.WriteStartElement( WSTrust13Constants.Prefix, WSTrust13Constants.ElementNames.RequestSecurityToken, WSTrust13Constants.NamespaceURI.NamespaceName );
            WriteKnownRequestElement( rst, writer );
            writer.WriteEndElement();
        }

        private static void WriteKeyType( XmlWriter writer, string keyType )
        {
            writer.WriteElementString( WSTrust13Constants.Prefix, WSTrust13Constants.ElementNames.KeyType, WSTrust13Constants.NamespaceURI.NamespaceName, keyType );
        }

        private static void WriteAppliesTo( XmlWriter writer, EndpointAddress appliesTo )
        {
            writer.WriteStartElement( prefixPolicy, AppliesTo, nsPolicy );
            writer.WriteStartElement( prefixAddressing, EndpointReference, nsAddressing );
            writer.WriteStartElement( prefixAddressing, Address, nsAddressing );
            writer.WriteString( appliesTo.Uri.AbsoluteUri );
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
        }

        private static void WriteEntropy( XmlWriter writer, byte[] entropyBytes )
        {
            if ( entropyBytes != null )
            {
                writer.WriteStartElement( WSTrust13Constants.ElementNames.Entropy, WSTrust13Constants.NamespaceURI.NamespaceName );
                writer.WriteStartElement( WSTrust13Constants.ElementNames.BinarySecret, WSTrust13Constants.NamespaceURI.NamespaceName );
                writer.WriteString( Convert.ToBase64String( entropyBytes ) );
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
            return;
        }

        private static void WriteKeySizeInBits( XmlWriter writer, int keySizeInBits )
        {
            writer.WriteElementString( WSTrust13Constants.Prefix, WSTrust13Constants.ElementNames.KeySize, WSTrust13Constants.NamespaceURI.NamespaceName, Convert.ToString( ( (int) keySizeInBits ), CultureInfo.InvariantCulture ) );
        }

        private static void WriteRequestType( XmlWriter writer, string requestType )
        {
            writer.WriteElementString( WSTrust13Constants.Prefix, WSTrust13Constants.ElementNames.RequestType, WSTrust13Constants.NamespaceURI.NamespaceName, requestType );
        }

    }

}